package com.woniuxy.ShiroIniTest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @Author：向天浩
 * @Date：2022/2/22 18:57
 * @Description :使用shiro进行登录认证
 */

public class RealmTest {
    public static void main(String[] args) {
        //使用BasicIniEnvironment读取我们的配置文件，创建该对象
        BasicIniEnvironment basicIniEnvironment = new BasicIniEnvironment("classpath:shiroini/shiro-realm.ini");

        //通过BasicIniEnvironment对象获取到securityManager
        SecurityManager securityManager = basicIniEnvironment.getSecurityManager();

        //将这个securityManager放到我们的工具类中SecurityUtils
        SecurityUtils.setSecurityManager(securityManager);

        //通过这个工具类获取到主体subject
        Subject subject = SecurityUtils.getSubject();
        //获取到账号和密码
        subject.login(new UsernamePasswordToken("root","123456"));

        //通过subject判断登录认证是否成功Authenticated;认证
        System.out.println("是否认证成功"+subject.isAuthenticated());

        //如果通过认证就进入下面方法
        if (subject.isAuthenticated()){
            //判断是否有这个角色
            if (subject.hasRole("role1")){
                System.out.println("进入了自定义域的shiro");
            }
        }
    }
}
